# Maintainer: Diego Sogari <diego.sogari@gmail.com>

_realname=mathgl
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.4.4
pkgrel=1
pkgdesc="Library for high-quality scientific graphics (mingw-w64)"
arch=('any')
url="https://mathgl.sourceforge.io/"
license=('LGPL')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake")
depends=("${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-fltk"
         "${MINGW_PACKAGE_PREFIX}-libharu"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-giflib"
         "${MINGW_PACKAGE_PREFIX}-qt5"
         "${MINGW_PACKAGE_PREFIX}-freeglut"
         "${MINGW_PACKAGE_PREFIX}-wxWidgets")
source=("${_realname}-${pkgver}.tar.gz::https://downloads.sourceforge.net/${_realname}/${_realname}-${pkgver}.tar.gz"
        "${_realname}-2.4.2.patch")
sha256sums=('0e5977196635962903eaff9b2f759e5b89108339b6e71427036c92bfaf3149e9'
            '3ee9d25a5de4db07ad43412eb69d95cfb7e6ef88f3ea4a5036810efd81ea9f9c')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Nbp1 -i "../${_realname}-2.4.2.patch"
}

build() {
  rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -Denable-lgpl=ON \
    "../${_realname}-${pkgver}"

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
# fixup references in associated cmake modules
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _ff in ${pkgdir}${MINGW_PREFIX}/lib/cmake/mathgl/*.cmake; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_ff}
  done
}
